package com.mew.game.slots;

import com.mew.common.entity.game.Jackport;
import com.mew.common.rest.game.BetAfterRequest;
import com.mew.common.rest.game.BetBeforeRequest;
import com.mew.common.rest.game.BetBeforeResponse;
import com.mew.common.rest.sas.PlaySlotsSasRequest;
import com.mew.game.strategy.StrategyService;
import com.zebra.common.concurrent.ZebraExecutor;
import com.zebra.common.concurrent.ZebraExecutorManager;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class SlotsBetStrategyController {

    @Autowired
    private StrategyService strategyDataService;



    @PostMapping(value = "/bet/after")
    @ResponseBody
    public Integer betAfter(@RequestBody BetAfterRequest request){
        log.info("Slots beAfter {}", request.toString());
        ZebraExecutor executor = ZebraExecutorManager.getInstance().getDisruptorByUserId(request.getUid());
        if (null != executor){
            executor.execute(() -> doDataBack(request));
        }else {
            doDataBack(request);
        }
        return 1;
    }

    private void doDataBack(BetAfterRequest request){
        try {
            strategyDataService.dataBack(request);
            EventBusAsyncCenter.post(PlaySlotsSasRequest.builder().userId(request.getUid()).roomId(request.getRoomId()).gameId(request.getGameId()).build()); //TODO play slots
        }catch (Exception e){
            log.error("Slots BetAfter Exception", e);
        }
    }

    @PostMapping(value = "/bet/jp")
    @ResponseBody
    public void ujp(@RequestBody Jackport jackport){
        strategyDataService.updateJackPort(jackport.getRoomId(),jackport);
    }

}
